package com.five.dao.impl.reception;

import com.five.dao.reception.UserDao;
import com.five.entity.backstage.TUser;
import com.five.util.DBUtil;
import javafx.scene.chart.ScatterChart;

import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    @Override
    public List<Map> queryAll() {
        String sql="select * from t_user";
        List<Map> maps = DBUtil.queryMap(sql);
        return maps;
    }

    @Override
    public int delUser(String uid) {
        String sql="delete from t_user where id=?";
        return DBUtil.manage(sql,uid);
    }

    @Override
    public int add(TUser u) {
        if(u.getId()==null){
            String sql="insert into t_user(name,pwd,idcard,phone)values(?,?,?,?)";
            return DBUtil.manage(sql,u.getName(),u.getPwd(),u.getIdcard(),u.getPhone());
        }else{
            String sql="update t_user set name=?, idcard=?,phone=? where id=?";
            return DBUtil.manage(sql,u.getName(),u.getIdcard(),u.getPhone(),u.getId());
        }
    }
    @Override
    public int register(TUser u) {
        String sql="insert into t_user(name,pwd,phone)values(?,?,?)";
        return DBUtil.manage(sql,u.getName(),u.getPwd(),u.getPhone());
    }
    @Override
    public Map findUserById(String uid) {
        String sql="select * from t_user where id=?";
        List<Map> maps = DBUtil.queryMap(sql, uid);
        Map m=maps.get(0);
        return m;
    }

    @Override
    public List<Map> findUserByIdCard(String idCard) {
        String sql="select * from t_user where idCard=?";
        List<Map> maps = DBUtil.queryMap(sql, idCard);
            return maps;
    }
    @Override
    public List<Map> findUserByPhone(String phone) {
        String sql="select * from t_user where phone=?";
        List<Map> maps = DBUtil.queryMap(sql, phone);
        return maps;
    }

    @Override
    public List<Map> findUserByNamePwd(String name,String pwd) {
        String sql="select * from t_user where name=? and pwd=?";
        List<Map> maps = DBUtil.queryMap(sql, name,pwd);
        return maps;
    }

    @Override
    public List<Map> findName(String name, String password) {
        String sql = "select * from t_user where phone=? and pwd=? and status =0";
        return DBUtil.queryMap(sql,name,password);
    }

    @Override
    public int updatae(String id, String pwd, String newpwd) {
        String sql = "UPDATE t_user set pwd=? WHERE id=? and pwd=?";
        return DBUtil.manage(sql,newpwd,id,pwd);
    }

}
